<?php
namespace Weixin\Controller;

use Think\Controller;
use Think\Page;

class WxImageNewsController extends Controller {
	
	public function __construct(){
		parent::__construct();
		$this->assign('serverUrl', get_url());
	}
	
	public function wxOneImgText() {
		
		// 跳转到单图文列表
		$where = array (
				'news_type' => 0 
		);
		$m = M ( 'wx_news_image_key' );
		$count = $m->where ( $where )->count ();
		import ( 'ORG.Util.Page' );
		$page = new Page ( $count, 3 );
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $m->order ( 'id desc' )->where ( $where )->limit ( $limit )->select ();
		
		if ($list) {
			foreach ( $list as $newKey => $newVal ) {
				$wx_news_image = M ( 'wx_news_image' );
				$where = array (
						'news_key' => $list [$newKey] ['news_key'] 
				);
				$newsArticle = $wx_news_image->where ( $where )->limit ( 8 )->select ();
				$list [$newKey] ['news_detail'] = $newsArticle;
			}
		}
		
		$this->assign ( 'newsImages', $list );
		$this->assign ( 'page', $page->show () );
		
		$this->display ( 'wx_news_one_list' );
	}
	public function addOneImgNews() {
		
		if (!IS_POST) {
			// 跳转到添加单图文页面
			$this->display ( "wx_news_add_one" );
			exit ();
		} else {
			// 处理添加单图文的post请求
			if (isset ( $_POST ['news_author'] )) {
				$news_author = $_POST ['news_author'];
			} else {
				$news_author = null;
			}
			if (isset ( $_POST ['news_zhaiyao'] )) {
				$news_zhaiyao = $_POST ['news_zhaiyao'];
			} else {
				$news_zhaiyao = null;
			}
			if (isset ( $_POST ['news_href'] )) {
				$news_href = $_POST ['news_href'];
			} else {
				$news_href = null;
			}
			$news_key = "key_" . time ();
			if (! empty ( $_POST ['is_show_article'] )) {
				$is_show_article = $_POST ['is_show_article'];
				$is_show_article = $is_show_article [0];
			} else {
				$is_show_article = 0;
			}
			$data = array (
					'news_title' => trim ( str_replace ( "'", "‘", $_POST ['news_title'] ) ),
					'news_author' => trim ( str_replace ( "'", "‘", $news_author ) ),
					'news_img_path' => $_POST ['news_img_path'],
					'is_show_article' => $is_show_article,
					'news_zhaiyao' => $news_zhaiyao,
					'news_content' => str_replace ( "'", "‘", $_POST ['content'] ),
					'news_href' => $news_href,
					'news_time' => time (),
					'news_key' => $news_key 
			);
			
			$data1 = array (
					'news_key' => $news_key,
					'news_type' => '0' 
			);
			$addFlag = M ( 'wx_news_image_key' )->add ( $data1 );
			
			if ($addFlag) {
				$addNewsId = M ( 'wx_news_image' )->add ( $data );
				// 调用生成静态文件的函数
				$target_html_file = "news_wx_id" . $addNewsId . ".html";
				$this->createNewsHtml ( $target_html_file, $data );
				// 生成静态文件
				if ($addNewsId) {
					$this->success ( '添加单图文成功！' );
				} else {
					$this->error ( '添加单图文失败！' );
				}
			} else {
				$this->error ( '添加单图文Key值失败！' );
			}
		}
	}
	
	/**
	 * 根据模板文件生成静态文件
	 * $target_html_file 目标文件
	 * $data 数据
	 */
	public function createNewsHtml($target_html_file, $data) {
		// 判断上传的图文是否添加了原文链接
		if (empty ( $data ['news_href'] )) {
			// 模板文件
			$tpl_file = C ( 'WEIXIN_SETTING_TPL_PATH' ) . "/Admin/tpl/content.tpl";
			$t_file = fopen ( $tpl_file, "r" );
			$target_file = fopen ( C ( 'WEIXIN_SETTING_TPL_PATH' ) . "/Admin/news/" . $target_html_file, "w" );
			while ( ! feof ( $t_file ) ) {
				$line_file = fgets ( $t_file );
				if (stristr ( $line_file, "<{title}>" )) {
					$line_file = str_replace ( "<{title}>", $data ['news_title'], $line_file );
				}
				if (stristr ( $line_file, "<{date}>" )) {
					$line_file = str_replace ( "<{date}>", date ( "Y-m-d H:i:s", $data ['news_time'] ), $line_file );
				}
				if (stristr ( $line_file, "<{content}>" )) {
					$news_content = str_replace ( "\\", "", $data ['news_content'] );
					$line_file = str_replace ( "<{content}>", $news_content, $line_file );
				}
				if (stristr ( $line_file, "<{wx_number}>" )) {
					$line_file = str_replace ( "<{wx_number}>", "美滴滴", $line_file );
				}
				fwrite ( $target_file, $line_file );
			}
			fclose ( $t_file );
			fclose ( $target_file );
		}
	}
	public function ajaxUploadImg() {
		// ajax上传封面图片,先删除原来图片
		if (isset ( $_FILES ['mypic'] )) {
			$picname = $_FILES ['mypic'] ['name'];
			$picsize = $_FILES ['mypic'] ['size'];
			if ($picname != "") {
				if ($picsize > 1024000) {
					echo '图片大小不能超过1M';
					exit ();
				}
				$type = strstr ( $picname, '.' );
				if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
					echo '图片格式不对！';
					exit ();
				}
				$rand = rand ( 100, 999 );
				$pics = date ( "YmdHis" ) . $rand . $type;
				// 上传路径
				$pic_path = C ( 'WEIXIN_SETTING_TPL_PATH' );
				
				$pic_dir = $pic_path . "/data/files/news";
				if (! is_dir ( $pic_dir )) {
					mkdir ( $pic_dir , 0777);
				}
				$pic_path = $pic_path . "/data/files/news/" . $pics;
				move_uploaded_file ( $_FILES ['mypic'] ['tmp_name'], $pic_path );
			}
			$size = round ( $picsize / 1024, 2 );
			$arr = array (
					'name' => $picname,
					'pic' => $pics,
					'size' => $size 
			);
			echo json_encode ( $arr );
		}
	}
	public function ajaxDeleteImg() {
		// ajax删除封面图片
		$filename = $_POST ['imagename'];
		if (stristr ( $filename, "http://" )) {
			$filename = substr ( $filename, strrpos ( $filename, "/" ) + 1, strlen ( $filename ) );
		}
		
		$pic_path = C ( 'WEIXIN_SETTING_TPL_PATH' );
		$pic_path = $pic_path . "/data/files/news/" . $filename;
		if (file_exists ( $pic_path )) {
			if (! empty ( $filename )) {
				unlink ( $pic_path );
				echo '1';
			} else {
				echo '删除失败.';
			}
		}
	}
	public function editNewsImage() {
		
		$news_key = $_GET ['news_key'];
		if (! IS_POST) {
			// 编辑单图文
			$where = array (
					'news_key' => $news_key 
			);
			$one_news_images = M ( 'wx_news_image' )->where ( $where )->limit ( 8 )->select ();
			
			$this->assign ( "one_news_images", $one_news_images [0] );
			$this->assign ( "image_path", substr ( $one_news_images [0] ['news_img_path'], strrpos ( $one_news_images [0] ['news_img_path'], "/" ) + 1, strlen ( $one_news_images [0] ['news_img_path'] ) ) );
			$this->display ( "wx_news_edit_one" );
			exit ();
		} else {
			// 处理添加单图文的post请求
			if (isset ( $_POST ['news_author'] )) {
				$news_author = $_POST ['news_author'];
			} else {
				$news_author = null;
			}
			if (isset ( $_POST ['news_zhaiyao'] )) {
				$news_zhaiyao = $_POST ['news_zhaiyao'];
			} else {
				$news_zhaiyao = null;
			}
			if (isset ( $_POST ['news_href'] )) {
				$news_href = $_POST ['news_href'];
			} else {
				$news_href = null;
			}
			if (! empty ( $_POST ['is_show_article'] )) {
				$is_show_article = $_POST ['is_show_article'];
				$is_show_article = $is_show_article [0];
			} else {
				$is_show_article = 0;
			}
			$id = $_POST ['id'];
			$data = array (
					'news_title' => trim ( str_replace ( "'", "‘", $_POST ['news_title'] ) ),
					'news_author' => trim ( str_replace ( "'", "‘", $news_author ) ),
					'news_img_path' => $_POST ['news_img_path'],
					'is_show_article' => $is_show_article,
					'news_zhaiyao' => $news_zhaiyao,
					'news_content' => str_replace ( "'", "‘", $_POST ['content'] ),
					'news_href' => $news_href,
					'news_time' => time () 
			);
			
			$where = array (
					'news_key' => $news_key 
			);
			$updateFlag = M ( 'wx_news_image' )->where ( $where )->save ( $data );
			
			if ($updateFlag) {
				// 调用生成静态文件的函数
				$target_html_file = "news_wx_id" . $id . ".html";
				$this->createNewsHtml ( $target_html_file, $data );
				$this->success ( '编辑单图文成功！' );
			} else {
				$this->error ( '编辑单图文失败！' );
			}
		}
	}
	public function ajaxDeleteNewsImage() {
		$news_key = $_GET ['news_key'];
		$image_path = $_GET ['image_path'];
		
		$where = array (
				'news_key' => "$news_key" 
		);
		$deleteFlag = M ( 'wx_news_image_key' )->where ( $where )->delete ();
		$deleteFlag2 = 0;
		if ($deleteFlag) {
			$deleteFlag2 = M ( 'wx_news_image' )->where ( $where )->delete ();
		}
		if ($deleteFlag2) {
			// 删除图片
			$news_image = substr ( $image_path, strrpos ( $image_path, "/" ) + 1, strlen ( $image_path ) );
			$pic_path = C ( 'WEIXIN_SETTING_TPL_PATH' );
			$pic_path = $pic_path . "/data/files/news/" . $news_image;
			if (file_exists ( $pic_path )) {
				if (! empty ( $news_image )) {
					unlink ( $pic_path );
				}
			}
			echo "1";
		} else {
			echo "0";
		}
		exit ();
	}
	public function wxMoreImgText() {
		$where = array (
				'news_type' => 1 
		);
		$m = M ( 'wx_news_image_key' );
		
		$count = $m->where ( $where )->count ();
		import ( 'ORG.Util.Page' );
		$page = new Page ( $count, 3 );
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $m->where ( $where )->order ( 'id desc' )->limit ( $limit )->select ();
		if ($list) {
			foreach ( $list as $newKey => $newVal ) {
				$where = array (
						'news_key' => $list [$newKey] ['news_key'] 
				);
				
				$newsArticle = M ( 'wx_news_image' )->where ( $where )->select ();
				;
				$list [$newKey] ['news_detail'] = $newsArticle;
			}
		}
		
		$this->assign ( "newsImages", $list );
		$this->assign ( "page", $page->show () );
		$this->display ( "wx_news_more_list" );
	}
	public function addMoreImgNews() {
		
		
		
		if (! IS_POST) {
			
			
			// 跳转到添加多图文页面
			$this->display ( "wx_news_add_more" );
			exit ();
		} else {
			$news_total = $_POST ['news_total'];
			if (isset ( $_GET ['news_key'] )) {
				if (! empty ( $_GET ['news_key'] )) {
					$news_key = $_GET ['news_key'];
				}
			} else {
				$news_key = "key_" . time ();
			}
			$data_arr = array ();
			$index = 0;
			for($i = 1; $i <= $news_total; $i ++) {
				if (! empty ( $_POST ['news_title' . $i . ''] )) {
					if (isset ( $_POST ['news_author' . $i . ''] )) {
						$news_author = $_POST ['news_author' . $i . ''];
					} else {
						$news_author = null;
					}
					if (isset ( $_POST ['news_href' . $i . ''] )) {
						$news_href = $_POST ['news_href' . $i . ''];
					} else {
						$news_href = null;
					}
					if (isset ( $_POST ['content' . $i . ''] )) {
						$content = $_POST ['content' . $i . ''];
					} else {
						$content = null;
					}
					if (! empty ( $_POST ['is_show_article' . $i . ''] )) {
						$is_show_article = $_POST ['is_show_article' . $i . ''];
						$is_show_article = $is_show_article [0];
					} else {
						$is_show_article = 0;
					}
					$data = array (
							'news_title' => trim ( str_replace ( "'", "‘", $_POST ['news_title' . $i . ''] ) ),
							'news_author' => trim ( str_replace ( "'", "‘", $news_author ) ),
							'news_img_path' => $_POST ['news_img_path' . $i . ''],
							'is_show_article' => $is_show_article,
							'news_href' => $news_href,
							'news_time' => time (),
							'news_content' => $content,
							'news_key' => $news_key 
					);
					$data_arr [$index] = $data;
					$index ++;
				}
			}
			
			if (isset ( $_GET ['news_key'] )) {
				if (! empty ( $_GET ['news_key'] )) {
					$news_key = $_GET ['news_key'];
					$wx_news_image_key = M ( 'wx_news_image_key' );
					$where = array (
							'news_key' => $news_key 
					);
					$deleteFlag = $wx_news_image_key->where ( $where )->delete ();
					$deleteFlag2 = 0;
					if (condition) {
						$deleteFlag2 = M ( 'wx_news_image' )->where ( $where )->delete ();
					}
					
					if ($deleteFlag2) {
						// 先删除以前的多图文后再添加
						$wx_news_image_key2 = M ( 'wx_news_image_key' );
						$datas = array (
								'news_key' => $news_key,
								'news_type' => 1 
						);
						$addFlag = $wx_news_image_key2->add ( $datas );
						
						if ($addFlag) {
							$wx_news_image = M ( 'wx_news_image' );
							$insert_id = 0;
							foreach ( $data_arr as $k1 => $v1 ) {
								$insert_id = $wx_news_image->add ( $v1 );
								if (empty ( $v1 ['news_href'] )) {
									$target_html_file = "news_wx_id" . $insert_id . ".html";
									$this->createNewsHtml ( $target_html_file, $v1 );
								}
							}
							
							if ($insert_id) {
								$this->success ( '编辑多图文成功！' );
							} else {
								$this->error ( '编辑多图文失败！' );
							}
						} else {
							$this->error ( '编辑多图文Key值失败！' );
						}
					}
				}
			} else {
				$wx_news_image_key2 = M ( 'wx_news_image_key' );
				$datas = array (
						'news_key' => $news_key,
						'news_type' => 1 
				);
				$addFlag = $wx_news_image_key2->add ( $datas );
				
				if ($addFlag) {
					$wx_news_image = M ( 'wx_news_image' );
					$insert_id = 0;
					foreach ( $data_arr as $k1 => $v1 ) {
						$insert_id = $wx_news_image->add ( $v1 );
						if (empty ( $v1 ['news_href'] )) {
							$target_html_file = "news_wx_id" . $insert_id . ".html";
							$this->createNewsHtml ( $target_html_file, $v1 );
						}
					}
					
					if ($insert_id) {
						$this->success ( '添加多图文成功！' );
					} else {
						$this->error ( '添加多图文失败！' );
					}
				} else {
					$this->error ( '添加多图文Key值失败！' );
				}
			}
		}
	}
	public function editNewsImageMore() {
		// 编辑多图文
		$news_key = $_GET ['news_key'];
		if (! IS_POST) {
			// 编辑单图文
			$where = array (
					'news_key' => $news_key 
			);
			$one_news_images = M ( 'wx_news_image' )->where ( $where )->limit ( 8 )->select ();
			
			foreach ( $one_news_images as $k => $v ) {
				$one_news_images [$k] ['image_path'] = substr ( $v ['news_img_path'], strrpos ( $v ['news_img_path'], "/" ) + 1, strlen ( $v ['news_img_path'] ) );
			}
			$this->assign ( "one_news_images", $one_news_images );
			$this->assign ( "count_data", count ( $one_news_images ) );
			$this->assign ( "news_key", $news_key );
			$this->display ( "wx_news_edit_more" );
			exit ();
		}
	}
}